package cn.jcby.app.lebu.service.impl;

import cn.jcby.app.lebu.controller.req.BasicListReq;
import cn.jcby.app.lebu.controller.res.ChargeLimitRes;
import cn.jcby.app.lebu.dao.ChargeLimitDao;
import cn.jcby.app.lebu.entity.ChargeLimitEntity;
import cn.jcby.app.lebu.service.ChargeLimitService;
import cn.jcby.app.lebu.utils.PageUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service("chargeLimitService")
public class ChargeLimitServiceImpl extends ServiceImpl<ChargeLimitDao, ChargeLimitEntity> implements ChargeLimitService {

    @Override
    public PageUtils queryPage(BasicListReq params) {
        IPage<ChargeLimitEntity> page = this.page(
                new Page<>(params.getPage(),params.getLimit()),
                Wrappers.<ChargeLimitEntity>lambdaQuery()
        );

        return new PageUtils(page);
    }
    @Override
    public void add(ChargeLimitEntity chargeLimitReq){
        ChargeLimitEntity chargeLimit = new ChargeLimitEntity() ;
        save(chargeLimit) ;
    }

    @Override
    public void updateBy(ChargeLimitEntity chargeLimitReq){
        ChargeLimitEntity chargeLimit = new ChargeLimitEntity() ;
        updateById(chargeLimit) ;
    }

    @Override
    public ChargeLimitRes getInfoById() {
        ChargeLimitRes res = new ChargeLimitRes();
        List<ChargeLimitEntity> list = this.list(Wrappers.<ChargeLimitEntity>lambdaQuery());
        Assert.isFalse(CollectionUtils.isEmpty(list), "未配置充值限制");
        ChargeLimitEntity chargeLimitEntity = list.get(0);
        res.setChargeLimit(chargeLimitEntity.getLimitSingleAmount().divide(BigDecimal.valueOf(100)).intValue());
        res.setChargeRatio(chargeLimitEntity.getRechargeRate().intValue());
        res.setWithdrawalRatio(chargeLimitEntity.getWithdrawalRate().intValue());
        return res;
    }
}